from rest_framework import serializers
from menu.models import SysMenu, SysRoleMenu
# 将菜单对象序列化为JSON格式

class SysMenuSerializer(serializers.ModelSerializer):
    children = serializers.SerializerMethodField() 
    
    def get_children(self, obj):
        if hasattr(obj, 'children'):
            serializersMenulist: list[SysMenuSerializer2] = list()
            for SysMenu in obj.children:
                serializersMenulist.append(SysMenuSerializer2(SysMenu).data)
            return serializersMenulist
    class Meta:
        model = SysMenu
        fields = "__all__"

class SysMenuSerializer2(serializers.ModelSerializer):
    class Meta:
        model = SysMenu
        fields = "__all__"
        
class SysRoleMenuSerializer(serializers.ModelSerializer):
    class Meta:
        model = SysRoleMenu
        fields = "__all__"